/** 
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 * 
 *SdProjectTeamController
 * SD项目任命-项目组成员
 * 
 * 修改纪录
 * 2021-12-08 版本：1.0 wangxinhua 创建。
 * @version 版本：1.0
 * @author 作者：wangxinhua
 * 创建日期：2021-12-08
 */

package com.topscomm.gpm.controller;

import java.util.List;
import java.util.Map;

import com.topscomm.gpm.controller.auto.GpmProjectTeamControllerAuto;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.gpm.pojo.GpmProjectAppointmentEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: wangxinhua
 * @date: 2021-12-08
 * @description:SD项目任命-项目组成员Controller
 * @modify:
 */
@RestController
@RequestMapping("sd/projectTeam")
public class GpmProjectTeamController extends GpmProjectTeamControllerAuto implements IGpmProjectTeamController {
	/**
	 * @author: wangxinhua
	 * @date: 2021-12-08
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("parentid")) {
			String value = paraMap.get("parentid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and appointmentid = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_teamType")) {
			String value = paraMap.get("qry_teamType").toString();
			if (!StringUtil.isEmpty(value) && !"0".equals(value))
				sql.appendSingle(" and teamid = {0} ", value);
		}
		if (paraMap.containsKey("qry_user")) {
			String value = paraMap.get("qry_user").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (usercode = '{0}' or username like '%{0}%') ", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_enabled")) {
			String value = paraMap.get("qry_enabled").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and enabled = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_appointmenttype")) {
			String value = paraMap.get("qry_appointmenttype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and appointmenttype in ({0}) ", value);
		}
		if (paraMap.containsKey("qry_projectcode")) {
			String value = paraMap.get("qry_projectcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and projectcode = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_teamIndex")) {
			String value = paraMap.get("qry_teamIndex").toString();
			String projectcode = paraMap.get("qry_projectcode").toString();
			if("1".equals(value)) {
				sql.appendSingle(" and appointmentid in (select id from {0} where version = (select max(NVL(version,1)) from {0} where projectcode = '{1}' and state = '9') and projectcode = '{1}' )", GpmProjectAppointmentEntity.tableName,projectcode);
			}
		}
		return sql.toString();
	}

	/**
	 * @author: wangxinhua
	 * @date: 2021-12-08
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	@GetMapping(value = "/queryList")
	public ResponseResult<Object> queryList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			int totalCount = this.service.getCount(sqlWhere);
			List<Map<String, Object>> tempList = null;
			if (query.getPageSize() > 0) {
				tempList = this.service.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
						query.getSidx(), query.getSord());
			} else {
				if (paraMap.containsKey("sidx") && paraMap.containsKey("sord")) {
					String sidx = paraMap.get("sidx").toString();
					String sord = paraMap.get("sord").toString();
					if (!StringUtil.isEmpty(sidx) && !StringUtil.isEmpty(sord))
						sqlWhere += " order by projectrole asc, " + sidx + " " + sord + ", id asc";
				}
				tempList = this.service.queryMapByWhere(sqlWhere);
			}
			// this.service.setDispFields(tempList);
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
}

